package com.google.coffeeshop.gui.desktop.receiptnote;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Date;
import java.util.Locale;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import vn.bang.utility.DateUtil;
import vn.bang.utility.LongUtil;

import com.google.coffeeshop.business.AvailabilityBusiness;
import com.google.coffeeshop.business.CodeGenerationBusiness;
import com.google.coffeeshop.business.ManagerBusiness;
import com.google.coffeeshop.entity.ReceiptNoteDTO;
import com.google.coffeeshop.entity.ReceiptNoteLineDTO;
import com.google.coffeeshop.gui.desktop.DatePicker;
import com.google.coffeeshop.gui.desktop.ObservingTextField;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class AddViewReceiptNoteFrame extends JFrame {
	private static final long serialVersionUID = 1L;
	
	private ObservingTextField tfCreatedDate;
	private Long receiptNoteId = null;
	private ReceiptNotePanel pnlReceiptNote;
	private JTextField tfCode;
	private ReceiptNoteLineTable tblReceiptNoteLine;
	private JLabel lblNewLabel_2;
	private JButton btnChooseCreatedDate;
	private JButton btnAdd;
	private JTextField tfTotalAmount;
	private JTextArea taDescription;
	private JLabel lblDescription;
	private JLabel lblTotal;
	private JButton btnAddReceiptNoteLine;
	private JButton btnEditReceiptLine;
	private ReceiptNoteDTO receiptNote;
	
	/**
	 * Create the panel.
	 */
	public AddViewReceiptNoteFrame(ReceiptNotePanel pnlReceiptNote, ReceiptNoteDTO receiptNote) {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosed(WindowEvent arg0) {
				removeReceiptNote();
			}

			
		});
		setSize(750, 606);
		setTitle("Receipt Note Details");
		this.receiptNote = receiptNote;
		this.pnlReceiptNote = pnlReceiptNote;
		getContentPane().setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(231dlu;default):grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(41dlu;default):grow"),},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("max(28dlu;default):grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("max(23dlu;default)"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("max(149dlu;default)"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		JLabel lblNewLabel = new JLabel("Code");
		getContentPane().add(lblNewLabel, "2, 2, right, default");
		
		tfCode = new JTextField();
		tfCode.setEditable(Boolean.FALSE);
		getContentPane().add(tfCode, "4, 2, 3, 1, fill, default");
		tfCode.setColumns(10);
		
		
		lblNewLabel_2 = new JLabel("Created Date");
		getContentPane().add(lblNewLabel_2, "2, 4, right, default");
		
		tfCreatedDate = new ObservingTextField();
		getContentPane().add(tfCreatedDate, "4, 4");
		tfCreatedDate.setEditable(Boolean.FALSE);
		tfCreatedDate.setColumns(10);
		tfCreatedDate.setText(DateUtil.toString(DateUtil.getNow()));
		
		btnChooseCreatedDate = new JButton("...");
		btnChooseCreatedDate.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				datePicker(tfCreatedDate);
			}
		});
		getContentPane().add(btnChooseCreatedDate, "6, 4");
		
		lblDescription = new JLabel("Description");
		getContentPane().add(lblDescription, "2, 6, right, top");
		
		taDescription = new JTextArea();
		getContentPane().add(taDescription, "4, 6, 3, 1, fill, fill");
		
		tblReceiptNoteLine = new ReceiptNoteLineTable();
		
		btnAddReceiptNoteLine = new JButton("Add Receipt Line");
		btnAddReceiptNoteLine.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				showAddEditReceiptNoteLineFrame(null);
			}
		});
		getContentPane().add(btnAddReceiptNoteLine, "2, 8");
		JScrollPane scrollPane = new JScrollPane(tblReceiptNoteLine);
		getContentPane().add(scrollPane, "4, 8, 3, 5, fill, fill");
		
		btnEditReceiptLine = new JButton("Edit Receipt Line");
		btnEditReceiptLine.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				showAddEditReceiptNoteLineFrame(tblReceiptNoteLine.getReceiptNoteLineSelected());
			}
		});
		getContentPane().add(btnEditReceiptLine, "2, 10");
		
		lblTotal = new JLabel("Total:");
		getContentPane().add(lblTotal, "4, 14, right, default");
		
		tfTotalAmount = new JTextField();
		tfTotalAmount.setText("0");
		tfTotalAmount.setEditable(Boolean.FALSE);
		getContentPane().add(tfTotalAmount, "6, 14, fill, default");
		tfTotalAmount.setColumns(10);
		
		btnAdd = new JButton("Add New Receipt Note");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				updateOrAddReceiptNote();
			}
		});
		getContentPane().add(btnAdd, "2, 16, 5, 1");
		
		if (receiptNote != null) {
			setReceiptNote(receiptNote);
			btnAdd.setVisible(Boolean.FALSE);
			btnAddReceiptNoteLine.setVisible(Boolean.FALSE);
			btnEditReceiptLine.setVisible(Boolean.FALSE);
			btnChooseCreatedDate.setVisible(Boolean.FALSE);
			tfTotalAmount.setEditable(Boolean.FALSE);
			taDescription.setEditable(Boolean.FALSE);
		} else {
			this.receiptNote = new ReceiptNoteDTO();
			this.receiptNote.setReceiptDate(DateUtil.dateFormat(DateUtil.getNow()));
			this.receiptNote.setAmount(0L);
			this.receiptNote.setCode(CodeGenerationBusiness.getReceiptNoteCode());
			tfCode.setText(CodeGenerationBusiness.getReceiptNoteCode());
			
		}
		
	}
	
	private void removeReceiptNote() {
		ManagerBusiness.removeReceiptNote(receiptNote.getId());
	}
	
	private void showAddEditReceiptNoteLineFrame(ReceiptNoteLineDTO receiptNoteLine) {
		AddEditReceiptNoteLineFrame frmAddEditReceiptNoteLine = new AddEditReceiptNoteLineFrame(this, receiptNote, receiptNoteLine);
		frmAddEditReceiptNoteLine.setVisible(Boolean.TRUE);
	}
	
	public void refreshParentGUI() {
		pnlReceiptNote.refreshGUI();
	}
	
	private void setReceiptNote(ReceiptNoteDTO receiptNote) {
		tfCode.setText(receiptNote.getCode());
		refreshReceiptNoteLineTable();
		tfTotalAmount.setText(String.valueOf(receiptNote.getAmount()));
		taDescription.setText(receiptNote.getDescription());
	}
	
	public void refreshReceiptNoteLineTable() {
		tblReceiptNoteLine.removeAllRow();
		tblReceiptNoteLine.addNewRows(AvailabilityBusiness.findReceiptNoteLinesByReceiptNoteId(receiptNote.getId()));
		tfTotalAmount.setText(String.valueOf(receiptNote.getAmount()));
	}
	
	private ReceiptNoteDTO getReceiptNoteDTO() {
		ReceiptNoteDTO receiptNote = this.receiptNote;
		receiptNote.setCode(tfCode.getText());
		receiptNote.setAmount(LongUtil.parseLong(tfTotalAmount.getText()));
		receiptNote.setDescription(taDescription.getText());
		receiptNote.setReceiptNoteLines(tblReceiptNoteLine.getReceiptNoteLineList());
		receiptNote.setReceiptDate(DateUtil.toDate(tfCreatedDate.getText()));
		return receiptNote;
	}
	
	private void updateOrAddReceiptNote() {
		ReceiptNoteDTO modifiedReceiptNote = getReceiptNoteDTO();
		AvailabilityBusiness.saveReceiptNote(modifiedReceiptNote);
		AvailabilityBusiness.warehousingProducts(modifiedReceiptNote);
		refreshParentGUI();
		disposeFrame();
	}
	
	private void disposeFrame() {
		this.dispose();
	}
	
	private Locale getLocale(String locate) {
		if(locate!=null && locate.length()>0)
			return new Locale(locate);
		else return Locale.FRANCE;
	}
	
	private void datePicker(ObservingTextField txtDate){
		String lang = null;
		final Locale loc = getLocale(lang);
		DatePicker dp = new DatePicker(txtDate, loc);
		Date selectedDate = dp.parseDate(txtDate.getText());
		dp.setSelectedDate(selectedDate);
		dp.start(txtDate);
	}
}
